package com.warm.androidstudy.utils;

import android.app.AppOpsManager;
import android.content.Context;
import android.os.Binder;
import android.os.Build;

import java.lang.reflect.Method;

/**
 * 检测大部分4.4以上5.0（21）以下的安卓机权限
 * Created by shenyq on 2017/7/9.
 */

public class MiUiCheckUtils {
    public MiUiCheckUtils() {
    }

    public static boolean isMiuiFloatWindowOpAllowed(Context context) {
        return Build.VERSION.SDK_INT >= 19?checkOp(context, 24):(context.getApplicationInfo().flags & 134217728) == 134217728;
    }

    public static boolean checkOp(Context context, int op) {
        if(Build.VERSION.SDK_INT >= 19) {
            AppOpsManager manager = (AppOpsManager)context.getSystemService("appops");

            try {
                Method e = manager.getClass().getDeclaredMethod("checkOp", new Class[]{Integer.TYPE, Integer.TYPE, String.class});
                int property = ((Integer)e.invoke(manager, new Object[]{Integer.valueOf(op), Integer.valueOf(Binder.getCallingUid()), context.getPackageName()})).intValue();
                LogUtil.e("0 invoke " + property);
                return 0 == property;
            } catch (Exception var5) {
                LogUtil.e(var5.getMessage());
            }
        } else {
            LogUtil.e("Below API 19 cannot invoke!");
        }

        return true;
    }
}

